package com.warm.pump.frame.gen.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;

;

/**
 * 业务逻辑异常
 *
 * @author
 */
public class ServiceException extends RuntimeException {
    protected String errorCode;
    protected String errorMessage;
    protected Object[] params;

    private static final Logger logger = LoggerFactory.getLogger(ServiceException.class);

    // 日志对象
    public ServiceException(String msg) {
        super(msg);
        logger.error(getStackTrace()[1].getMethodName() + ": 事务回滚!错误原因:" + msg);
    }

    public ServiceException(Object className, Throwable cause) {
        super(className.toString(), cause);

        if (cause.getMessage().indexOf("Duplicate") > 0) {
            throw new DuplicateKeyException("记录重复", cause);
        }
        logger.error(cause.getStackTrace()[0].getMethodName() + ": 事务回滚!错误原因:" + cause.getMessage());
    }

    public ServiceException(Exception e) {
        super(e);
        /*
         * String message = null; for(int i=0;i<e.getStackTrace().length;i++){ message += e.getStackTrace()[i] +
         * "\r\n"; }
         */
        StringBuilder message = new StringBuilder();
        for (int i = 0; i < e.getStackTrace().length; i++) {
            message.append(e.getStackTrace()[i] + "\r\n");
        }
        // message消息转换

        // 打印Exception堆栈错误信息
        logger.error(message.toString());

        logger.error(getStackTrace()[1].getMethodName() + ": 事务回滚!错误原因:" + e.getMessage());
    }

    public ServiceException(String code, String msg, Exception e) {
        super(msg, e);
        errorCode = code;
    }

    public ServiceException(String code, String msg, Exception e, Object[] params) {
        super(msg, e);
        errorCode = code;
        params = params;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    private static final long serialVersionUID = -903912364023186153L;
}
